<?php
namespace app\models;
use common\component\tool\Tool;
use app\models\Basic;

use Yii;

class CoreConfig extends Basic
{
    
    public static function tableName()
    {
        return '{{%core_config}}';
    }

    public function rules()
    {
        return [
            ['app_id', 'required', 'message' => 'app_id不能为空', 'on' => 'add'],
            ['app_id', 'integer', 'message' => 'app_id必须为数字，或数字字符串', 
                        'min'=>0, 'tooSmall' => 'app_id最小存储0', 
                        'max'=>18446744073709551615, 'tooBig' => 'app_id最大存储18446744073709551615'],
            ['pc_url', 'default', 'value' => ''],
            ['pc_url', 'string', 'message' => 'pc_url必须是字符串',
                                'min' => 2, 'tooShort' => 'pc_url最短2位',
                                'max' => 32, 'tooLong' => 'pc_url最长32位'],
            ['mobile_url', 'default', 'value' => ''],
            ['mobile_url', 'string', 'message' => 'mobile_url必须是字符串',
                                'min' => 2, 'tooShort' => 'mobile_url最短2位',
                                'max' => 32, 'tooLong' => 'mobile_url最长32位'],
            ['server', 'integer', 'message' => 'server必须为数字，或数字字符串', 
                        'min'=>0, 'tooSmall' => 'server最小存储0', 
                        'max'=>20, 'tooBig' => 'server最大存储20'],
            ['logo', 'default', 'value' => ''],
            ['logo', 'string', 'message' => 'logo必须是字符串',
                                'min' => 2, 'tooShort' => 'logo最短2位',
                                'max' => 512, 'tooLong' => 'logo最长512位'],
            ['hotline', 'default', 'value' => ''],
            ['hotline', 'string', 'message' => 'hotline必须是字符串',
                                'min' => 2, 'tooShort' => 'hotline最短2位',
                                'max' => 16, 'tooLong' => 'hotline最长16位'],
            ['icp', 'default', 'value' => ''],
            ['icp', 'string', 'message' => 'icp必须是字符串',
                                'min' => 2, 'tooShort' => 'icp最短2位',
                                'max' => 16, 'tooLong' => 'icp最长16位'],
            ['traffic_statistics', 'default', 'value' => ''],
            ['traffic_statistics', 'string', 'message' => 'traffic_statistics必须是字符串'],
            ['state', 'default', 'value' => 2],
            ['state', 'integer', 'message' => 'state必须为数字，或数字字符串'],
            ['state', 'in', 'range' => array(1, 2), 'message' => 'state值必须在1,2中选择'],
            ['close_reason', 'default', 'value' => ''],
            ['close_reason', 'string', 'message' => 'close_reason必须是字符串',
                                'min' => 2, 'tooShort' => 'close_reason最短2位',
                                'max' => 32, 'tooLong' => 'close_reason最长32位'],
            ['lastmodify_time', 'default', 'value' => '1000-01-01 00:00:00'],
            ['lastmodify_time', 'date', 'format'=>'yyyy-MM-dd HH:mm:ss', 'message' => 'lastmodify_time非yyyy-MM-dd HH:mm:ss格式'],
        ];
    }


    /*添加*/
    public function addConfig($data, $app_id)
    {
        $data['CoreConfig']['lastmodify_time'] = date('Y-m-d H:i:s', time());
        // P($data);
        if($this->load($data) and $this->validate()){
            if($this->save(false)){
                $this->addDomainLog($data, $app_id);
                return true;
            }
        }
        return false;
    }

    /*
    修改
    $data       修改的数据
    $app_id 
    */
    public function modifyConfig($data, $app_id)
    {
        if(isset($data['CoreConfig']['pc_url_agreement']) 
            and isset($data['CoreConfig']['pc_url'])
            and !empty($data['CoreConfig']['pc_url'])
            ){
            $data['CoreConfig']['pc_url'] = $data['CoreConfig']['pc_url_agreement'] . $data['CoreConfig']['pc_url'];
        }
        if(isset($data['CoreConfig']['mobile_url_agreement']) 
            and isset($data['CoreConfig']['mobile_url'])
            and !empty($data['CoreConfig']['mobile_url'])
            ){
            $data['CoreConfig']['mobile_url'] = $data['CoreConfig']['mobile_url_agreement'] . $data['CoreConfig']['mobile_url'];
        }
        if(isset($data['CoreConfig']['state'])){
            $data['CoreConfig']['state'] = Tool::changeBool($data['CoreConfig']['state']);
        }
        if(isset($data['CoreConfig']['logo']) and is_array($data['CoreConfig']['logo'])){
            $data['CoreConfig']['logo'] = Tool::changeUploadFormat($data['CoreConfig']['logo']);
        }
        $coreConfig = self::find()->where('app_id = :id', [':id' => $app_id])->one();
        /*如果不存在，则需要添加一条*/
        if(empty($coreConfig)){
            $this->scenario = 'add';
            $coreConfig = new CoreConfig;
            $data['CoreConfig']['app_id'] = $app_id;
            // P($data);
            if($coreConfig->addConfig($data, $app_id)){
                return true;
            }else{
                if($coreConfig->hasErrors()){
                    return $coreConfig->getErrors();
                }
            }
            return false;
        }
        if(!is_null($coreConfig)){
            /*旧的网址，为了是保存日志*/
            $oldInfo['pc_url'] = $coreConfig->pc_url;
            $oldInfo['mobile_url'] = $coreConfig->mobile_url;
            $oldInfo['server'] = $coreConfig->server;

            if($this->load($data) and $this->validate()){
                if(!empty($this->pc_url)){
                    $coreConfig->pc_url = $this->pc_url;
                }
                if(!empty($this->mobile_url)){
                    $coreConfig->mobile_url = $this->mobile_url;
                }
                if(!empty($this->server)){
                    $coreConfig->server = $this->server;
                }else{
                    $coreConfig->logo = '';
                }
                if(!empty($this->logo)){
                    $coreConfig->logo = $this->logo;
                }else{
                    $coreConfig->logo = '';
                }
                if(!empty($this->hotline)){
                    $coreConfig->hotline = $this->hotline;
                }
                if(!empty($this->icp)){
                    $coreConfig->icp = $this->icp;
                }
                if(!empty($this->traffic_statistics)){
                    $coreConfig->traffic_statistics = $this->traffic_statistics;
                }
                if(!empty($this->state)){
                    $coreConfig->state = $this->state;
                }
                if(!empty($this->close_reason)){
                    $coreConfig->close_reason = $this->close_reason;
                }
                $coreConfig->lastmodify_time = date('Y-m-d H:i:s', time());
                // P($coreConfig);
                if($coreConfig->save(false)){
                    $this->addDomainLog($data, $app_id, 2, $oldInfo);
                    return true;
                }
            }else{
                if($this->hasErrors()){
                    return $this->getErrors();
                }
            }
        };
        return false;
    }


    /*
    如果有域名改动，需要添加日志
    flag 1说明是新添加的域名，不需要判断数据是否是新的
    */
    public function addDomainLog($data, $app_id, $flag = 1, $oldData = '')
    {
        $DomainBindLog = new DomainBindLog;
        $pcUrl = isset($data['CoreConfig']['pc_url'])?$data['CoreConfig']['pc_url']:'';
        $mobileUrl = isset($data['CoreConfig']['mobile_url'])?$data['CoreConfig']['mobile_url']:'';
        $server = isset($data['CoreConfig']['server'])?$data['CoreConfig']['server']:0;
        $pass = true;
        if($flag == 1){
            if(empty($pcUrl) and empty($mobileUrl)){
                $pass = false;
            };
            $logData = [
                'DomainBindLog' => [
                    'app_id' => $app_id,
                    'old_pc_domain' => '',
                    'new_pc_domain' => $pcUrl,
                    'old_mobile_domain' => '',
                    'new_mobile_domain' => $mobileUrl,
                    'old_server' => 0,
                    'new_server' => $server,
                ]
            ];
            // P($logData);
        }else{
            $oldPcUrl = isset($oldData['pc_url'])?$oldData['pc_url']:'';
            $oldMobileUrl = isset($oldData['mobile_url'])?$oldData['mobile_url']:'';
            $oldServer = isset($oldData['server'])?$oldData['server']:0;
            // P($pcUrl . '-' . $oldPcUrl);
            if(($pcUrl != $oldPcUrl) or ($mobileUrl != $oldMobileUrl) or ($server != $oldServer)){
                $logData = [
                    'DomainBindLog' => [
                        'app_id' => $app_id,
                        'old_pc_domain' => $oldPcUrl,
                        'new_pc_domain' => $pcUrl,
                        'old_mobile_domain' => $oldMobileUrl,
                        'new_mobile_domain' => $mobileUrl,
                        'old_server' => $oldServer,
                        'new_server' => $server,
                    ]
                ];
            }else{
                $pass = false;
            }
            // P($data);
        }
        
        if($pass){
            $res = $DomainBindLog->addLog($logData);
            // P($res);
        }
    }

    
}